package com.xm.xmapp.fragment;

import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.VolleyError;
import com.xm.xmapp.LoginActivity;
import com.xm.xmapp.MainActivity;
import com.xm.xmapp.PileOrderDetailActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.app.Type;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.customview.TipDialog;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.myinterface.MainActivityInterFace;
import com.xm.xmapp.myinterface.MyDialogButton;
import com.xm.xmapp.myinterface.ScrollInterFaceAble;
import com.xm.xmapp.request.bean.Charing_statingRequest;
import com.xm.xmapp.request.bean.Stop_chargeRequest;
import com.xm.xmapp.response.bean.BaseBeanResponse;
import com.xm.xmapp.response.bean.CustomHeightBean;
import com.xm.xmapp.response.bean.ResponseCharingStating;
import com.xm.xmapp.response.bean.ResponseStartCharge;
import com.xm.xmapp.tools.StringTools;
import com.xm.xmapp.tools.TimeUtil;
import com.xm.xmapp.tools.TimeUtil.TimeListener;
import com.xm.xmapp.tools.Tools;

import static com.xm.xmapp.R.id.rl_btn;


public class PileUseFragment extends BaseFragment implements OnClickListener,ScrollInterFaceAble {

    TextView tvAddress;
    TextView tvCost;
    TextView tvPower;
    TextView tvCharginTime;
    TextView tvChargingNumber;
    RelativeLayout rlBtn;
    LinearLayout bottom;
    LinearLayout layout;
    ResponseStartCharge start_chargeBean;
    private Rect mRect;
//    private TextView name, Soc, time, money, finishbtn, charging_pile_number;
    GetDataInterFace<String> listener;
    Charing_statingRequest requestBean;
    DisssmissInterFace in;
    ResponseCharingStating bean;
    private boolean isCancel = false;
    /**
     * 定时任务，每个一段时间获取订单金额
     */
    TimeUtil task = new TimeUtil(2 * MyAppcation.getMyAppcation().get_money_second * 1000,
            MyAppcation.getMyAppcation().get_money_second * 1000);

    @Override
    public void onPause() {
        super.onPause();
        task.cancel();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        task.cancel();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MyAppcation.getMyAppcation().getUserBean() != null && !isCancel)
            task.start();
    }

    public void setStart_chargeBean(ResponseStartCharge start_chargeBean) {
        this.start_chargeBean = start_chargeBean;
    }

    @Override
    public int setContentView() {
        return R.layout.fragment_pile_use;
    }

    @Override
    public void ViewTreeObserver() {

        super.ViewTreeObserver();
        CustomHeightBean bean = new CustomHeightBean();
        bean.ExitOffset = layout.getHeight();
        bean.MaxOffset = bottom.getHeight() - rlBtn.getHeight();
        initRect();
        bean.flag = MainActivityInterFace.INTT_STATUS_NO_ONCLICK;
        MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE, bean);
    }

    @Override
    public void initView() {

        layout = (LinearLayout) findViewById(R.id.layout);
        bottom = (LinearLayout) findViewById(R.id.bottom);
        tvAddress = (TextView) findViewById(R.id.tv_address);
        tvPower = (TextView) findViewById(R.id.tv_power);
        tvCharginTime = (TextView) findViewById(R.id.tv_chargin_time);
        tvCost = (TextView) findViewById(R.id.tv_cost);
        rlBtn = (RelativeLayout) findViewById(rl_btn);
        tvChargingNumber = (TextView) findViewById(R.id.tv_charging_number);
    }

    @Override
    public void initData() {

        if (promptDialog != null)
            promptDialog.setBackKeyDownIsDismiss(false);
        requestBean = new Charing_statingRequest();
        requestBean.orderNo = start_chargeBean.orderNo;
        listener = new GetDataInterFace<String>() {

            @Override
            public void onResponse(String response) {
                promptDialog.dismiss();
                BaseBeanResponse<String> baseBean = JSON.parseObject(response,
                        new TypeReference<BaseBeanResponse<String>>() {
                        });
                switch (baseBean.errCode) {
                    case 0: {
                        bean = JSON.parseObject(baseBean.data, ResponseCharingStating.class);

                        tvAddress.setText(bean.charging_pile_network_name);
                        tvPower.setText(bean.Soc + "度");
                        tvCharginTime.setText(StringTools.getTime(bean.CharginTime / 60));
                        tvCost.setText(bean.TotalMoney + "元");
                        tvChargingNumber.setText(bean.charging_pile_number);
                        switch (bean.status) {
                            case 1:

                                break;
                            case 2:

                                Intent intent = new Intent(MyAppcation.getMyAppcation(), PileOrderDetailActivity.class);
                                intent.putExtra("orderId", bean.orderNo);
                                intent.putExtra("status", true);
                                startActivity(intent);
                                finishCharge();
                                break;
                            case 3:
                                task.cancel();
                                isCancel = true;
                                Tools.showMessage(bean.statusDesc);
                                break;
                            case 4:
                                Tools.showMessage(bean.statusDesc);
                                break;

                            default:
                                break;
                        }

                        break;
                    }
                    case -999:
                        task.cancel();
                        isCancel = true;
                        MyAppcation.getMyAppcation().clearUserBean();
                        if (!MyAppcation.getMyAppcation().haveActivity(LoginActivity.class)) {

                            Intent intent = new Intent(MyAppcation.getMyAppcation(), LoginActivity.class);
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            MyAppcation.getMyAppcation().startActivity(intent);

                        }
                        break;
                    case -1:
                        task.cancel();
                        isCancel = true;
                        Tools.showMessage(baseBean.errMsg);
                        break;
                    default:
                        task.cancel();
                        isCancel = true;
                        Tools.showMessage(baseBean.errMsg);
                        break;
                }

            }
        };

        in = new DisssmissInterFace() {

            @Override
            public void putError(VolleyError error) {
                promptDialog.dismiss();
            }
        };
        if (task != null) {
            task.setTimeListener(new TimeListener() {

                @Override
                public void timeOnTick(long time) {
                    MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, listener, in);
                }

                @Override
                public void timeFinish() {
                    task.cancel();
                    task.start();
                }
            });
            promptDialog.show();
            task.start();
        }

    }

    @Override
    public void initEvent() {
        rlBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case rl_btn:

                if (bean != null) {

                    TipDialog tipDialog = new TipDialog(getActivity(), "温馨提示", "立即结束充电吗?", new MyDialogButton() {

                        @Override
                        public void setTitle(int flag, String str) {

                            Stop_chargeRequest requestBean = new Stop_chargeRequest();
                            requestBean.orderNo = bean.orderNo;
                            requestBean.connectorId = bean.charging_pile_number;
                            promptDialog.show();
                            MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean,
                                    new GetDataInterFace<String>() {

                                        @Override
                                        public void onResponse(String response) {
                                            promptDialog.dismiss();
                                            Intent intent = new Intent(MyAppcation.getMyAppcation(),
                                                    PileOrderDetailActivity.class);
                                            intent.putExtra("orderId", bean.orderNo);
                                            intent.putExtra("status", true);
                                            startActivity(intent);
                                            finishCharge();

                                        }
                                    }, new DisssmissInterFace() {

                                        @Override
                                        public void putError(VolleyError error) {
                                            promptDialog.dismiss();

                                        }
                                    });
                        }
                    });
                    tipDialog.show();
                }
                break;

            default:
                break;
        }
    }

    public void finishCharge() {

        task.cancel();
        isCancel = true;
        CustomHeightBean heightBean = new CustomHeightBean();
        heightBean.flag = MainActivityInterFace.STOP_CHANGING;
        MyAppcation.getMyAppcation().notifyDataSetChanged(MainActivity.class, Type.A_Main.CHANGE_UI_CODE, heightBean);
    }

    @Override
    public void setOffext(int currentProgress) {

        if(rlBtn!=null && mRect!=null) {
            rlBtn.layout(mRect.left, -currentProgress + mRect.top, mRect.right, mRect.bottom - currentProgress);
        }
    }

    @Override
    public void initRect() {
        if(mRect==null) {
            int left = rlBtn.getLeft();
            int top = rlBtn.getTop();
            int right = rlBtn.getRight();
            int bottom = rlBtn.getBottom();
            mRect = new Rect(left, top, right, bottom);
        }
    }
}
